package com.lf.sp.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.lf.sp.common.ServerResponse;
import com.lf.sp.dao.ShippingMapper;
import com.lf.sp.pojo.Shipping;
import com.lf.sp.service.IShippingService;

@Service("iShippingService")
public class ShippingServiceImpl implements IShippingService{

	@Autowired
	private ShippingMapper shippingMapper;
	//新增
	@Override
	public ServerResponse add(Integer userId,Shipping shipping){
		shipping.setUserId(userId);
		int resultCount = shippingMapper.insert(shipping);
		if(resultCount > 0){
			Map result = Maps.newHashMap();
			result.put("shippingId", shipping.getId());
			return ServerResponse.createBySuccess("添加成功", result);
		}
			return ServerResponse.createByErrorMessage("新建地址失败");
	}
	//删除
	@Override
	public  ServerResponse del(Integer userId,Integer shippingId){
		int resultCount = shippingMapper.deleteByShippingIdUserId(shippingId, userId);
		if(resultCount > 0){
			return ServerResponse.createBySuccessMessage("删除成功");
		}
		return ServerResponse.createByErrorMessage("删除失败");
				
	}
	//修改
	@Override
	public ServerResponse update(Integer userId,Shipping shipping){
		shipping.setUserId(userId);
		int resultCount = shippingMapper.updateByShipping(shipping);
		if(resultCount > 0){
			return ServerResponse.createBySuccessMessage("更新成功");
		}
		return ServerResponse.createByErrorMessage("更新失败");
		
	}
	//查询
	@Override
	public ServerResponse select(Integer userId,Integer shippingId){
		Shipping shipping = shippingMapper.selectByShippingIdUserId(shippingId, userId);
		if(shipping == null){
			return ServerResponse.createByErrorMessage("无法查询到该地址");
		}
		return ServerResponse.createBySuccess("查询成功", shipping);
	}
	
	//分页查询
	@Override
	public ServerResponse list(Integer userId,int pageNum,int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<Shipping> shippingList = shippingMapper.selectByUserId(userId);
		PageInfo pageInfo = new PageInfo(shippingList);
		return ServerResponse.createBySuccess(pageInfo);
	}
	
}
